import type { ArtColumn } from 'ali-react-table';
import { Form, Input, InputNumber, Tooltip } from 'antd';
import DiyInputNumber from './components/DiyInputNumber';

const getColumns = (columns: ArtColumn[]): ArtColumn[] => {
  return [
    {
      name: '序号',
      code: 'number',
      width: 100,
      align: 'center',
      getCellProps: (value, row, index) => {
        if (row.number === '合计') {
          return {
            colSpan: 4,
          };
        }
        return {};
      },
    },
    {
      name: '场站名称',
      code: 'stationName',
      width: 200,
      align: 'center',
      getCellProps: (value, row, index) => {
        if (row.number === '合计') {
          return {
            colSpan: 0,
          };
        }
        return {};
      },
    },
    {
      name: '装机',
      code: 'stationCapacity',
      width: 120,
      align: 'center',
      getCellProps: (value, row, index) => {
        if (row.number === '合计') {
          return {
            colSpan: 0,
          };
        }
        return {};
      },
    },
    {
      name: '是否异常',
      code: 'eorror',
      width: 90,
      align: 'center',
      getCellProps: (value, row, index) => {
        if (row.number === '合计') {
          return {
            colSpan: 0,
          };
        }
        return {};
      },
      render: (value) => {
        return value ? (
          <Tooltip title="设备问题">
            <span className="tips-font">是</span>
          </Tooltip>
        ) : (
          '否'
        );
      },
    },
    {
      name: '已上网电量',
      align: 'center',
      children: [
        ...columns,
        {
          name: '合计',
          align: 'center',
          code: 'sum',
          width: 100,
        },
      ],
    },
  ];
};

const getAddColumns = (columns: ArtColumn[]): ArtColumn[] => {
  return [
    {
      name: '日期',
      code: 'date',
      width: 100,
      align: 'center',
    },
    {
      name: '已上网电量',
      align: 'center',
      children: [
        ...columns,
        {
          name: '合计',
          align: 'center',
          code: 'sum',
          width: 90,
          render: (value, row, index) => (
            <Form.Item name={[`dataList@${index}`, 'sum']}>
              <Input readOnly className="read-only-no-border" />
            </Form.Item>
          ),
        },
      ],
    },
  ];
};

const buildTable = (sumBody: any, head: any[], dataList: any[]) => {
  let columns: ArtColumn[] = [];
  let dataSource: any[] = [];

  if (head.length > 0 && dataList.length > 0) {
    const dynamicsHead: ArtColumn[] = [];

    head.forEach((v) => {
      dynamicsHead.push({
        name: v.name,
        align: 'center',
        children: [{ code: v.code, name: v.data, align: 'center' }],
      });
    });

    if (dynamicsHead.length > 0) {
      columns = getColumns(dynamicsHead);
    }
    const last = {
      ...sumBody,
      number: '合计',
    };

    dataSource = [...dataList, last];
  }

  return {
    columns,
    dataSource,
  };
};

const buildChartsData = (head: any[], dataList: any[]) => {
  const xAxisData: string[] = [];
  const chartsData: any = {};

  if (head.length > 0 && dataList.length > 0) {
    dataList.forEach((v) => {
      head.forEach((k) => {
        if (xAxisData.length !== head.length) {
          xAxisData.push(k.name);
        }
        if (!chartsData[v.stationName]) {
          chartsData[v.stationName] = [];
        }
        chartsData[v.stationName].push(v[k.code]);
      });
    });
  }

  return {
    xAxisData,
    chartsData,
  };
};

const buildAddTable = (head: any[], dataList: any[]) => {
  let columns: ArtColumn[] = [];
  let dataSource: any[] = [];

  if (head.length > 0 && dataList.length > 0) {
    const dynamicsHead: ArtColumn[] = [];
    const lastObj: any = {};

    head.forEach((v) => {
      lastObj[v.code] = undefined;
      dynamicsHead.push({
        name: v.name,
        align: 'center',
        children: [
          {
            code: v.code,
            name: v.data,
            align: 'center',
            render: (value, row, index) =>
              row.date !== '合计' ? (
                <Form.Item name={[`dataList@${index}`, v.code]}>
                  <DiyInputNumber />
                </Form.Item>
              ) : (
                <Form.Item name={[`dataList@${index}`, v.code]}>
                  <Input readOnly className="read-only-no-border" />
                </Form.Item>
              ),
          },
        ],
      });
    });

    if (dynamicsHead.length > 0) {
      columns = getAddColumns(dynamicsHead);
    }
    const last = {
      ...lastObj,
      date: '合计',
    };

    dataSource = [...dataList, last];
  }

  return {
    columns,
    dataSource,
  };
};

export { getColumns, buildTable, buildChartsData, getAddColumns, buildAddTable };
